package process;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import abstractclass.AbstractProcess;
import annotation.Caption;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import util.StringUilts;

public class JspProcess extends AbstractProcess{

	@Override
	public void processJsp(Object obj, String path, String tableCaption) {
		if(obj==null||path==null){
			throw new NullPointerException();
		}
		path = path+"jsp"+File.separator;
		File file = new File(path);
		if(!file.exists()){
			file.mkdirs();
		}
		Writer writer = null;
		Writer writer2 = null;
		Writer writer3 = null;
		
		 try {  
	            
	            Template template = configuration.getTemplate(jspFilePrefix+"input.jsp");
	            Template template2 = configuration.getTemplate(jspFilePrefix+"list.jsp");
	            Template template3 = configuration.getTemplate(jspFilePrefix+"view.jsp");
	            
	            Map<String, Object> inputParamMap = new HashMap<String, Object>();
	            Map<String, Object> listParamMap = new HashMap<String, Object>();
	           

	            
	            Map<String,Object> captions = new  HashMap<String, Object>();
	            Map<String,Object> listCaptions = new  HashMap<String, Object>();
	            
	            //List fields = new ArrayList();
	            Map<String, Object> fields = new HashMap<String, Object>();
	            
	            Field[] fs =  obj.getClass().getDeclaredFields();
	            for (Field f : fs) {
	            	Caption caption = f.getAnnotation(Caption.class);
	            	if(null!=caption){
	            		if(f.getName().equals("seqId")){
		            		inputParamMap.put("seqId", f.getName());
		            		inputParamMap.put("seqIdValue",prefix+f.getName()+suffix);
		            	}else{
		            		captions.put(f.getName(),caption.value());
		            	}
	            		fields.put( f.getName(),prefix+f.getName()+suffix);
	            		listCaptions.put(prefix+f.getName()+suffix, caption.value());
	            	}
				}
	            //Input
	            inputParamMap.put("fields", fields); 
	            inputParamMap.put("captions", captions); 
	            inputParamMap.put("lowercaseBean", StringUilts.initLowerCase(obj.getClass().getSimpleName()));
	            inputParamMap.put("path","${path}");
	            
	            //List
	            listParamMap.put("listCaptions", listCaptions); 
	            listParamMap.put("page", "${page.results}"); 
	            listParamMap.put("fields", fields);
	            listParamMap.put("listName", tableCaption);
	            listParamMap.put("pageNumber", "${page.pageNumber}");
	            listParamMap.put("totalPages", "${page.totalPages}");
	            listParamMap.put("search","Like"); 
	            listParamMap.put("path","${path}");
	            listParamMap.put("lowercaseBean", StringUilts.initLowerCase(obj.getClass().getSimpleName()));
	            //File file = new File("");
	            //FileOutputStream fos = new FileOutputStream(file);
	            writer = new OutputStreamWriter(new FileOutputStream(path+ StringUilts.initLowerCase(obj.getClass().getSimpleName())+"Input.jsp"),"UTF-8");
	            writer2 = new OutputStreamWriter(new FileOutputStream(path+ StringUilts.initLowerCase(obj.getClass().getSimpleName())+"List.jsp"),"UTF-8");
	            writer3 = new OutputStreamWriter(new FileOutputStream(path+ StringUilts.initLowerCase(obj.getClass().getSimpleName()+"View.jsp")),"UTF-8");

	            template.process(inputParamMap, writer); 
	            template2.process(listParamMap, writer2);
	            template3.process(inputParamMap, writer3); 
	            
	            
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } catch (TemplateException e) {  
	            e.printStackTrace();  
	        }finally{
	        	try 
	        	{
		        	if(writer!=null){
						writer.close();
		        	}
		        	if(writer2!=null){
		        		writer.close();
		        	}
		        	if(writer3!=null){
		        		writer.close();
		        	}
		        	
	        	} catch (IOException e) 
	        	{
					e.printStackTrace();
				}
	        	
	        }
	}


	
}
